package com.hue.springbootcompletion.controller;

import com.hue.springbootcompletion.config.ElasticSearchConfig;
import org.apache.lucene.search.Query;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.PrefixQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.List;

/**
 * @Author: 姚凤桥
 * @Date: 2022/4/15 15:00
 * @Description: 智能提示
 */
@RestController
@RequestMapping("completion")
public class CompletionController {
    @Autowired
    private RestHighLevelClient esClient;

    @GetMapping("/query")
    public List<String> query(@RequestParam("key") String key) throws IOException {
        //构建查询条件
        BoolQueryBuilder qb = QueryBuilders.boolQuery();
        PrefixQueryBuilder queryBuilder = QueryBuilders.prefixQuery("title", "java");
        qb.should(queryBuilder);

        //构建查询源
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(qb);

        SearchRequest searchRequest = new SearchRequest("job").source(sourceBuilder);

        SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);


        return null;
    }
}
